﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace EHR.Ctrls.QueryDesign
{
    public class ViewDesignInfo : IXmlItem
    {
        public string ViewName { get; set; }
        public string TableName { get; set; }
        public IList<ColInfo> Columns { get; set; }
        public IList<FilterInfo> Filters { get; set; }
        public IList<OrderByInfo> OrderBys { get; set; }

        public ViewDesignInfo()
        {
            Columns = new List<ColInfo>();
            Filters = new List<FilterInfo>();
            OrderBys = new List<OrderByInfo>();
        }

        public XElement ToXml()
        {
            return ToXml(Columns);
        }

        public XElement ToXml(IList<ColInfo> columns)
        {
            XElement el = new XElement("View",
                new XAttribute("ViewName", ViewName),
                new XAttribute("TableName", TableName));

            el.Add(ColumnsToXml(columns));
            el.Add(FiltersToXml());
            el.Add(OrderBysToXml());
            return el;
        }

        private XElement ColumnsToXml(IList<ColInfo> columns)
        {
            XElement el = new XElement("Columns");
            foreach (ColInfo f in columns)
            {
                el.Add(f.ToXml());
            }

            return el;
        }
        
        private XElement FiltersToXml()
        {
            XElement el = new XElement("Filters");
            foreach (FilterInfo f in Filters)
            {
                el.Add(f.ToXml());
            }

            return el;
        }

        private XElement OrderBysToXml()
        {
            XElement el = new XElement("OrderBys");
            foreach (OrderByInfo order in OrderBys)
            {
                el.Add(order.ToXml());
            }

            return el;
        }

        public void FromXml(XElement el)
        {
            ViewName = el.Attribute("ViewName").Value;
            TableName = el.Attribute("TableName").Value;

            ColumnsFromXml(el.Element("Columns"));
            FiltersFromXml(el.Element("Filters"));
            OrderBysFromXml(el.Element("OrderBys"));
        }

        private void ColumnsFromXml(XElement el)
        {
            foreach (XElement xCol in el.Elements())
            {
                ColInfo ci = new ColInfo();
                ci.FromXml(xCol);
                Columns.Add(ci);
            }
        }

        private void FiltersFromXml(XElement el)
        {
            foreach (XElement xFilter in el.Elements())
            {
                FilterInfo fi = new FilterInfo();
                fi.FromXml(xFilter);
                Filters.Add(fi);
            }
        }

        private void OrderBysFromXml(XElement el)
        {
            foreach (XElement xOrder in el.Elements())
            {
                OrderByInfo orderInfo = new OrderByInfo();
                orderInfo.FromXml(xOrder);
                OrderBys.Add(orderInfo);
            }
        }
    }
}
